# coding=utf-8
"""
author: jamon
"""

from typing import List

import pygame
import sys

from pgu import gui
from pygame.event import Event

from client.model.global_data import GlobalData
from client.views.base import ScreenBase, register_screen
from client.views.scene_mgr import scene_mgr
from client.config import ClientConfig
from pygame.surface import Surface

from obespoir.share.espoir_log import global_log as logger

rgb_white = (255, 255, 255)


@register_screen("login")
class LoginScene(ScreenBase):

    def __init__(self, screen: Surface):
        super(LoginScene, self).__init__(screen=screen)
        self.config = ClientConfig()
        self.background_img: Surface = pygame.image.load(
            self.config.login_bg_path)
        self.text_background_img: Surface = pygame.image.load(
            self.config.text_background)

        self.account_label_img: Surface = pygame.image.load(
            self.config.account_name_label)
        self.password_label_img: Surface = pygame.image.load(
            self.config.password_label_path)
        self.login_label: Surface = pygame.image.load(
            self.config.login_label_path)

        self.account_label: gui.Button = gui.Button(
            "", width=62, height=18, background=self.account_label_img)
        self.password_label: gui.Button = gui.Button(
            "", width=62, height=18, background=self.password_label_img)
        self.account_txt: gui.Input = gui.Input(
            background=self.text_background_img)
        self.password_txt: gui.Password = gui.Password(
            background=self.text_background_img)

        self.app = gui.App()
        self.is_closed: bool = False

    def init_screen(self, *args, **kwargs):
        width, height = self.config.width, self.config.height
        container = gui.Container(width=width, height=height)
        # container.add(account_name, 190, 205)
        container.add(self.account_label, 300, 170)
        container.add(self.account_txt, 240, 200)

        # container.add(password_label, 190, 305)
        container.add(self.password_label, 300, 270)
        container.add(self.password_txt, 240, 300)

        # btn_login = gui.Button("Login")
        btn_login = gui.Button("", background=self.login_label, width=110,
                               height=35)
        btn_login.connect(gui.CLICK, self.login)
        container.add(btn_login, 275, 360)

        self.app.init(widget=container, area=(0, 0, 0, 0))

    async def update(self, events: List[Event] = None, **kwargs):
        if self.is_closed:
            return
        if events is None:
            events = []
        for event in events:
            if pygame.QUIT == event.type:
                pygame.quit()
                sys.exit()
            self.app.event(event)
        self._screen.blit(self.background_img, (0, 0))
        self.app.paint(screen=self._screen)
        # pygame.display.update()
        # await asyncio.sleep(1.0 / 60)

    def login(self):
        user_name = self.account_txt.value
        password = self.password_txt.value
        if not user_name or not password:
            return

        GlobalData().cache_login_info(user_name=self.account_txt.value,
                                      password=self.password_txt.value)
        self.switch_other_screen()

    def switch_other_screen(self):
        self.is_close = True
        new_screen = "begin"
        logger.info("start", new_screen=new_screen,
                    user_name=self.account_txt.value,
                    password=self.password_txt.value)
        scene_mgr.switch_to_scene(new_screen)
